home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / circuits / irsim-ca.2 / irsim-ca / irsim-cap-9.2 / src / utils / Export / export.c < prev    next >
C/C++ Source or Header  |  1994-10-17  |  5KB  |  272 lines

  1. /*
  2.  * Create an exports file from the implementation files.
  3.  *
  4.  * Items declared as public are copied to the new file.
  5.  * Initializations and array sizes are omitted (e.g. "int x = 3" will
  6.  * output "int x;", "char a[ 100 ];" will output "char a[];"
  7.  *
  8.  * Normally a temporary definitions file is created and compared to
  9.  * the given destination.  If they are different, the temporary file
  10.  * is copied on top of the destination.  This is so that dependencies
  11.  * when using "make" are not triggered.
  12.  *
  13.  */
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <signal.h>
  18. #include <stdarg.h>
  19.  
  20. #define    procedure    void
  21. #define    and        &&
  22. #define    or        ||
  23. #define    not        !
  24.  
  25. typedef enum { FALSE, TRUE } Boolean;
  26. typedef char *String;
  27.  
  28.  
  29. String        tmpname;
  30. char        *ModuleName();
  31. procedure    abnorm();
  32. FILE        *fin, *fout;
  33.  
  34. void Crash(String m1, ...);
  35. void Process(String modname);
  36.  
  37. int main( argc, argv )
  38.   int argc;
  39.   String argv[];
  40.   {
  41.     extern  String mktemp();
  42.     String  outfname;
  43.     char    syscmd[ 512 ];
  44.     FILE    *tmp;
  45.     int r;
  46.     int i;
  47.  
  48.     if( argc < 3 )
  49.     Crash( "usage: %s file.c [ file.c ] output\n", ModuleName( *argv ) );
  50.  
  51.     signal( SIGINT, abnorm );
  52.     signal( SIGQUIT, abnorm );
  53.  
  54.     tmpname = mktemp( "/tmp/exportXXXXXX" );
  55.     fout = fopen(tmpname, "w");
  56.     if( fout == NULL )
  57.     Crash( "can't write %s", tmpname );
  58.     
  59.     outfname = argv[ --argc ];
  60.     for( i = 1; i < argc; i++ )
  61.       {
  62.     if( ( fin = fopen( argv[ i ], "r" ) ) == NULL )
  63.         Crash( "can't read %s\n", argv[ i ] );
  64.     Process( ModuleName( argv[ i ] ) );
  65.     fclose( fin );
  66.       }
  67.     fclose( fout );
  68.     (void) sprintf( syscmd, "cmp -s %s %s", tmpname, outfname );
  69.     r = system( syscmd );
  70.     if (r != 0)
  71.       {
  72.     (void) sprintf( syscmd, "mv %s %s", tmpname, outfname );
  73.     r = system( syscmd );
  74.     if( r != 0 )
  75.         fprintf( stderr, "can't create %s\n", outfname );
  76.       }
  77.     else
  78.     unlink( tmpname );
  79.     exit(0);
  80.   }
  81.  
  82.  
  83.  
  84. String ModuleName( s )
  85.   String s;
  86.   {
  87.     String r, p;
  88.     static char buf[ 256 ];
  89.  
  90.     for( r = s; *r != '\0'; r++ );
  91.     while( r > s and *r != '/' ) r--;
  92.     if( *r == '/' ) r++;
  93.     p = buf;
  94.     while( *r ) *p++ = *r++;
  95.     *p = '\0';
  96.     return( buf );
  97.   }
  98.  
  99.  
  100. char  buff[ 1025 ];
  101. char  *line = &buff[1];
  102.  
  103. void Process( modname )
  104.   String  modname;
  105.   {
  106.     register    String    p;
  107.     register    Boolean    isproc;
  108.     register    Boolean    isarray;
  109.     register    Boolean    firstIndex;
  110.     Boolean        header;
  111.  
  112.     header = FALSE;
  113.     while( (p = fgets( line, 1024, fin )) != NULL )
  114.       {
  115.     if( *p == 'p' and strncmp( p, "public", 6 ) == 0 )
  116.       {
  117.         isproc = FALSE;
  118.         isarray = FALSE;
  119.         firstIndex = TRUE;
  120.         if( not header )
  121.           {
  122.         fprintf( fout, "\n\t/* EXPORTS FROM %s */\n\n", modname );
  123.         header = TRUE;
  124.           }
  125.         if( p[ 6 ] == '\n' )
  126.           {
  127.         CopyMacro();
  128.         continue;
  129.           }
  130.         else if( not (p[6] <= ' ' ) )
  131.         continue;
  132.         if( !strncmp( &p[ 7 ], "typedef", 7 ) )
  133.           {
  134.         CopyDef( &p[ 7 ] );
  135.         goto finish;
  136.           }
  137.         fputs( "extern ", fout );
  138.         for( p += 7; *p != ';'; p++ )
  139.           {
  140.         switch( *p )
  141.           {
  142.             case '(':
  143.             if( p[-1] > ' ' )
  144.               {
  145.                 isproc = TRUE;
  146.                 fputs( "( /* ", fout );
  147.               }
  148.             else
  149.                 fputc( *p, fout );
  150.             break;
  151.             case ')' :
  152.             if( isproc )
  153.               {
  154.                 fputs( "*/ )", fout );
  155.                 isproc = FALSE;
  156.                 goto finish;
  157.               }
  158.             else
  159.                 putc( *p, fout );
  160.             break;
  161.             case '[' :
  162.             isarray = TRUE;
  163.             if( firstIndex )
  164.                 fputs( "[ /*", fout );
  165.             else
  166.                 fputs( "[ ", fout );
  167.             break;
  168.             case ']' :
  169.             if( isarray )
  170.               {
  171.                 if( firstIndex )
  172.                 fputs( "*/ ]", fout );
  173.                 else
  174.                 fputs( " ]", fout );
  175.                 firstIndex = FALSE;
  176.                 isarray = FALSE;
  177.               }
  178.             else
  179.                 putc( *p, fout );
  180.             break;
  181.             case '=' :
  182.             goto finish;
  183.             break;
  184.             case '\n' :
  185.             p = fgets( line, 1024, fin );
  186.             if( p == NULL )
  187.                 goto finish;
  188.             else
  189.                 p--;
  190.             break;
  191.             default :
  192.             putc( *p, fout );
  193.           }
  194.           }
  195.         finish :
  196.         fputs( ";\n", fout );
  197.       }
  198.       }
  199.   }
  200.  
  201.  
  202. CopyDef( s )
  203.   char  *s;
  204.   {
  205.     register    String    p;
  206.     register    int    pCount = 0;
  207.  
  208.     p = s;
  209.     do
  210.       {
  211.     putc( *p, fout );
  212.     if( *p == '{' )
  213.         pCount++;
  214.     else if( *p == '}' )
  215.         pCount--;
  216.     else if( *p == '\n' )
  217.       {
  218.         p = fgets( line, 1024, fin );
  219.         if( p == NULL )
  220.         return;
  221.         else
  222.         p--;
  223.       }
  224.     p++;
  225.       }
  226.     while( not (*p == ';' and pCount == 0) );
  227.   }
  228.  
  229.  
  230. CopyMacro()
  231.   {
  232.     register    String    p;
  233.     
  234.     while( (p = fgets( line, 1024, fin )) != NULL )
  235.       {
  236.     fputs( line, fout );
  237.     while( *p != '\n' ) p++;
  238.     p--;
  239.     if( *(p) != '\\' )
  240.         return;
  241.       }
  242.   }
  243.  
  244.  
  245.  
  246. /*
  247.  * Terminate program.
  248.  */
  249. procedure abnorm( signo )
  250. int signo;
  251.   {
  252.     if( fout )
  253.     fclose( fout );
  254.     unlink( tmpname );
  255.     exit( signo );
  256.   }
  257.  
  258.  
  259. /* VARARGS1 */
  260. void Crash(String m1, ... )
  261. {
  262.     va_list args;
  263.  
  264.     fprintf( stderr, m1, args);
  265.     if( fout != NULL )
  266.       {
  267.     fclose( fout );
  268.     unlink( tmpname );
  269.       }
  270.     exit( -1 );
  271. }
  272.